<template>
  <view>
    <view class="goods_list" v-if="goodsList.length!==0">
      <view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
        <my-goods :item="item"></my-goods>
      </view>
    </view>

    <view class="good_ove" v-else>暂无数据</view>
    <view class="gotohende" @click="tohend" v-if="ltb>=500">
      <uni-icons type="arrow-up" size="30"></uni-icons>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        // 商品列表数据
        goodsList: [],
        // 总条数 
        total: 0,
        isloading: false,
        // 距离头部的距离
        ltb: 0

      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      // 获取商品数据列表请求
      async getGoodsList(cb) {
        // 打开节流阀
        this.isloading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // 请求完成，关闭节流阀
        this.isloading = false
        cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        // console.log(res);
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },

      onPageScroll: function(res) {
        this.ltb = res.scrollTop
      },
      // 回到头部
      tohend() {
        uni.pageScrollTo({
          scrollTop: 0,
          duration: 300,

        })
      },
      // 点击每一项，跳转至详情页
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    // 下拉刷新
    onPullDownRefresh() {
      // 重置关键数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []
      // 重新发起数据请求
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    // 上拉加载
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
      if (this.isloading) return
      this.queryObj.pagenum++
      this.getGoodsList()
    },

  }
</script>

<style lang="scss">
  .good_ove {
    text-align: center;
    line-height: 100vh;
  }

  .gotohende {
    width: 30px;
    height: 30px;
    position: fixed;
    top: 80%;
    right: 10%;

    .uni-icons {
      background-color: brown;
    }
  }
</style>
